# app/db/init_task_groups.py
import logging
logger = logging.getLogger(__name__)
from app.db import models

PRE_DEFINED_TASK_GROUPS = [
    {
        "group_name": "every_1m",
        "description": "每1分钟执行",
        "schedule_type": "interval",
        "schedule_value": {"minutes": 1},
        "is_active": True
    },
    {
        "group_name": "hourly",
        "description": "每小时执行",
        "schedule_type": "interval",
        "schedule_value": {"hours": 1},
        "is_active": True
    },
    {
        "group_name": "daily_01:00",
        "description": "每天凌晨1点执行",
        "schedule_type": "cron",
        "schedule_value": {"hour": 1, "minute": 0},
        "is_active": True
    },
    {
        "group_name": "daily_09:00",
        "description": "每天上午9点执行",
        "schedule_type": "cron",
        "schedule_value": {"hour": 9, "minute": 0},
        "is_active": True
    },
    {
        "group_name": "weekly_mon_09:00",
        "description": "每周一上午9点执行",
        "schedule_type": "cron",
        "schedule_value": {"day_of_week": "mon", "hour": 9, "minute": 0},
        "is_active": True
    }
]


def init_predefined_task_groups(db):
    """初始化预定义任务组"""
    for group_data in PRE_DEFINED_TASK_GROUPS:
        # 检查是否已存在
        existing = db.query(models.AlertTaskGroup).filter(
            models.AlertTaskGroup.group_name == group_data["group_name"]
        ).first()

        if not existing:
            task_group = models.AlertTaskGroup(**group_data)
            db.add(task_group)
            logger.info(f"创建预定义任务组: {group_data['group_name']}")

    db.commit()